package com.example.interceptor.demos.config;

import com.example.interceptor.demos.common.AppVar;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2023-12-04
 * Time:20:09
 * Text：自定义拦截器
 */
@Configuration
public class UserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //业务方法
        HttpSession session=request.getSession(false);
        if(session!=null && session.getAttribute(AppVar.SESSION_KEY)!=null){
            //用户已登陆
            return true;
        }
        return false;
    }
}
